package com.example.springboot_demo.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.cache.MapCache;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.example.springboot_demo.pojo.KaiMaiExportProduct;
import com.example.springboot_demo.pojo.KaiMaiPhoto;
import com.example.springboot_demo.pojo.KaiMaiProduct;
import com.example.springboot_demo.pojo.SchoolZone;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class KaimaiService {

    public static void dealKaimai() {
        List<KaiMaiProduct> products = new ArrayList<>();
        EasyExcel.read(new File("/Users/jiangan/Desktop/mytproduct.xlsx"), KaiMaiProduct.class, new AnalysisEventListener<KaiMaiProduct>() {

            @Override
            public void invoke(KaiMaiProduct batchWxActInfo, AnalysisContext analysisContext) {
                if (StringUtils.isNotBlank(batchWxActInfo.getBarcode()) && !"kg".equals(batchWxActInfo.getUnit())) {
                    products.add(batchWxActInfo);
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).readCache(new MapCache()).doReadAll();


        List<KaiMaiPhoto> photo = new ArrayList<>();
        EasyExcel.read(new File("/Users/jiangan/Desktop/mytmedia.xlsx"), KaiMaiPhoto.class, new AnalysisEventListener<KaiMaiPhoto>() {

            @Override
            public void invoke(KaiMaiPhoto batchWxActInfo, AnalysisContext analysisContext) {
                if ("PRODUCT_DETAIL_IMAGE".equals(batchWxActInfo.getMedia_type())) {
                    batchWxActInfo.setMedia_type("MAIN");
                } else {
                    batchWxActInfo.setMedia_type("DETAIL");
                }
                photo.add(batchWxActInfo);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).readCache(new MapCache()).doReadAll();


        Map<String, List<KaiMaiPhoto>> nameMap = photo.stream().collect(Collectors.groupingBy(KaiMaiPhoto::getAlias));
//        Map<String, List<KaiMaiPhoto>> pinyinMap = photo.stream().collect(Collectors.groupingBy(KaiMaiPhoto::getProductPinyin));


        ArrayList<KaiMaiExportProduct> list = new ArrayList<>();
        for (KaiMaiProduct product : products) {
            if (nameMap.get(product.getAlias()) != null) {
                List<KaiMaiPhoto> kaiMaiPhotos = nameMap.get(product.getAlias());
                if (CollectionUtils.isNotEmpty(kaiMaiPhotos)) {
                    System.out.println(product.getTitle()+"____1____"+product.getAlias());
                    for (KaiMaiPhoto kaiMaiPhoto : kaiMaiPhotos) {
                        if ("MAIN".equals(kaiMaiPhoto.getMedia_type())) {;
                            if (product.getMainPhotos() == null) {
                                product.setMainPhotos(new ArrayList<>());
                            }
                            product.getMainPhotos().add(kaiMaiPhoto.getMedia_url());
                        } else {
                            if (product.getDetailPhotos() == null) {
                                product.setDetailPhotos(new ArrayList<>());
                            }
                            product.getDetailPhotos().add(kaiMaiPhoto.getMedia_url());
                        }
                    }
                }
            }
            KaiMaiExportProduct export = new KaiMaiExportProduct();
            BeanUtils.copyProperties(product,export);
            export.setSale_type(1);
            if (CollectionUtils.isNotEmpty(product.getMainPhotos())) {
                export.setMain_images(JSON.toJSONString(product.getMainPhotos()));
            }
            if (CollectionUtils.isNotEmpty(product.getDetailPhotos())) {
                export.setDetail_images(JSON.toJSONString(product.getDetailPhotos()));
            }
                list.add(export);

//            else if (pinyinMap.get(product.getProductPinyin()) != null) {
//                List<KaiMaiPhoto> kaiMaiPhotos = pinyinMap.get(product.getProductPinyin());
//                if (CollectionUtils.isNotEmpty(kaiMaiPhotos)) {
//                    System.out.println(product.getTitle()+"____2____"+kaiMaiPhotos.get(0).getAlias());
//                    for (KaiMaiPhoto kaiMaiPhoto : kaiMaiPhotos) {
//                        if ("MAIN".equals(kaiMaiPhoto.getMedia_type())) {
//                            if (product.getMainPhotos() == null) {
//                                product.setMainPhotos(new ArrayList<>());
//                            }
//                            product.getMainPhotos().add(kaiMaiPhoto.getMedia_url());
//                        } else {
//                            if (product.getDetailPhotos() == null) {
//                                product.setDetailPhotos(new ArrayList<>());
//                            }
//                            product.getDetailPhotos().add(kaiMaiPhoto.getMedia_url());
//                        }
//                    }
//                }
//            }
        }
        EasyExcel.write(new File("/Users/jiangan/Desktop/exportKaimai.xlsx"), KaiMaiExportProduct.class).sheet("sheet1").doWrite(list);
    }


    public static void main(String[] args) {
//        dealKaimai();
        dealExcel();
    }
    public static void dealExcel() {
        List<KaiMaiExportProduct> products = new ArrayList<>();
        EasyExcel.read(new File("/Users/jiangan/Desktop/exportKaimai.xlsx"), KaiMaiExportProduct.class, new AnalysisEventListener<KaiMaiExportProduct>() {

            @Override
            public void invoke(KaiMaiExportProduct batchWxActInfo, AnalysisContext analysisContext) {
                products.add(batchWxActInfo);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).readCache(new MapCache()).doReadAll();

        List<List<KaiMaiExportProduct>> partition = Lists.partition(products, 20000);
        int i = 0;
        for (List<KaiMaiExportProduct> kaiMaiExportProducts : partition) {
            String filename = "/Users/jiangan/Desktop/import_" + i + ".xls";
            EasyExcel.write(new File(filename), KaiMaiExportProduct.class).sheet("sheet1").doWrite(kaiMaiExportProducts);
            i++;
        }
    }

}
